package com.example.apiproject.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.apiproject.dao.JoinActivityDao;
import com.example.apiproject.dao.UserDao;
import com.example.apiproject.entity.Activities;
import com.example.apiproject.dao.ActivitiesDao;
import com.example.apiproject.entity.Articles;
import com.example.apiproject.entity.PostEntity;
import com.example.apiproject.entity.form.AddActivityForm;
import com.example.apiproject.entity.form.PostActivitiesEntity;
import com.example.apiproject.jwt.jwtutil;
import com.example.apiproject.service.ActivitiesService;
import com.example.apiproject.utils.AppPageUtils;
import com.example.apiproject.utils.JsonUtils;
import com.example.apiproject.utils.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.PageImpl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * (Activities)表服务实现类
 *
 * @author makejava
 * @since 2023-09-23 13:40:56
 */
@Service("activitiesService")
public class ActivitiesServiceImpl implements ActivitiesService {
    @Resource
    private ActivitiesDao activitiesDao;
    @Autowired
    private jwtutil jwtUtils;
    @Autowired
    private UserDao userDao;

    @Autowired
    private JoinActivityDao joinActivityDao;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Activities queryById(String id) {
        return this.activitiesDao.queryById(id);
    }

    /**
     * 分页查询
     * @param activities 筛选条件
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public AppPageUtils queryByPage(Activities activities, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Activities> list = activitiesDao.queryAllByLimit(activities);
        List<PostActivitiesEntity> postNewList=new ArrayList<>();
        for (Activities item : list) {
            BeanUtil.copyProperties(item,PostActivitiesEntity.class);
            PostActivitiesEntity postActivitiesEntity = new PostActivitiesEntity();
            BeanUtils.copyProperties(item,postActivitiesEntity);
            postActivitiesEntity.setCreateTime(item.getCreatetime());
            postActivitiesEntity.setImages(JsonUtils.JsonToList(item.getMedia()));
            postActivitiesEntity.setNickName(userDao.queryNameById(item.getUserid()));
            postActivitiesEntity.setAvater(userDao.queryAvatarById(item.getUserid()));
            postNewList.add(postActivitiesEntity);
        }

        return new AppPageUtils(postNewList,(int)activitiesDao.count(activities),pageSize,pageNum);

    }

    @Override
    public int insert(AddActivityForm addActivityForm, HttpServletRequest request) {
        String token = request.getHeader("token");
        if(StringUtils.isBlank(token)){
            token = request.getParameter("token");
        }
//        System.out.println(token);
        int  userId = jwtUtils.getUserId(token);
        Activities activities =new Activities();
        activities.setUserid(userId);
        UUID uuid = UUID.randomUUID();
        activities.setId(uuid.toString());
        /* Id改为自增*/
        activities.setStatus(0);
        activities.setCreatetime(LocalDateTime.now());
        activities.setStartTime(addActivityForm.getStartTime());
        activities.setEndTime(addActivityForm.getEndTime());
        activities.setTitle(addActivityForm.getTitle());
        activities.setDes(addActivityForm.getContent());
        activities.setAddress(addActivityForm.getAddress());
        activities.setSname(addActivityForm.getSname());
        activities.setSidentynumber(addActivityForm.getSidentyNumber());
        activities.setSphone(addActivityForm.getSphone());
        activities.setValue(addActivityForm.getValue());
        List<String> l=addActivityForm.getMedia();

        activities.setMedia(JSON.toJSONString(l));
        activities.setType(addActivityForm.getType());
        int id =activitiesDao.insert(activities);
        return  id;
    }


    /**
     * 修改数据
     *
     * @param activities 实例对象
     * @return 实例对象
     */
    @Override
    public int update(Activities activities) {

        return  activitiesDao.update(activities);

    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.activitiesDao.deleteById(id) > 0;
    }

    @Override
    public AppPageUtils queryPageListBytype(Integer pageNum, Integer pageSize, Integer type) {
        PageHelper.startPage(pageNum,pageSize);
        List<Activities> activities = activitiesDao.queryAllBytype(type);
        List<PostActivitiesEntity> postNewList=new ArrayList<>();
        activities.forEach(l->{
            PostActivitiesEntity postActivitiesEntity = new PostActivitiesEntity();
            BeanUtils.copyProperties(l,postActivitiesEntity);
            postActivitiesEntity.setStartTime(l.getStartTime());
            postActivitiesEntity.setCreateTime(l.getCreatetime());
            postActivitiesEntity.setImages(JsonUtils.JsonToList(l.getMedia()));
            postActivitiesEntity.setNickName(userDao.queryNameById(l.getUserid()));
            postActivitiesEntity.setAvater(userDao.queryAvatarById(l.getUserid()));

            postNewList.add(postActivitiesEntity);
        });
        Activities activity= new Activities();
        activity.setType(type);

        long count = activitiesDao.count(activity);
        AppPageUtils appPage=new AppPageUtils(postNewList,(int)count,pageSize,pageNum);

        return appPage;
    }


    /**
     * 根据用户id查询用户参与的活动

     * @return 活动集合
     */
    @Override
    public AppPageUtils queryByUserId(HttpServletRequest request,Activities activities,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        String token = request.getHeader("token");
        if(StringUtils.isBlank(token)){
            token = request.getParameter("token");
        }
        int  userId = jwtUtils.getUserId(token);

        Page<Activities> page = activitiesDao.queryAllByUserIdAndLimit(activities,userId);
        List<PostActivitiesEntity> postNewList=new ArrayList<>();
        for (Activities item : page) {
            PostActivitiesEntity postActivitiesEntity = new PostActivitiesEntity();
            BeanUtils.copyProperties(item,postActivitiesEntity);
            postActivitiesEntity.setImages(JsonUtils.JsonToList(item.getMedia()));
            postActivitiesEntity.setNickName(userDao.queryNameById(item.getUserid()));
            postActivitiesEntity.setAvater(userDao.queryAvatarById(item.getUserid()));
            postNewList.add(postActivitiesEntity);
        }


        return new AppPageUtils(postNewList,(int)page.getTotal(),pageSize,pageNum);
    }

    @Override
    public List<PostActivitiesEntity> queryRecent(HttpServletRequest request) {
        String token = request.getHeader("token");
        if(StringUtils.isBlank(token)){
            token = request.getParameter("token");
        }
        int  userId = jwtUtils.getUserId(token);
        List<Integer> ids = joinActivityDao.queryActIdRecent(userId);
        List<Activities> activitiesList=new ArrayList<>();
        if(ids.size()==0){ activitiesList = activitiesDao.queryRecentnull( LocalDateTime.now(),userId);}
        else {
             activitiesList = activitiesDao.queryRecent(ids, LocalDateTime.now(), userId);
        }
        List<PostActivitiesEntity> postNewList=new ArrayList<>();
        for (Activities item : activitiesList) {
            PostActivitiesEntity postActivitiesEntity = new PostActivitiesEntity();
            BeanUtils.copyProperties(item,postActivitiesEntity);
            postActivitiesEntity.setImages(JsonUtils.JsonToList(item.getMedia()));
            postActivitiesEntity.setNickName(userDao.queryNameById(item.getUserid()));
            postActivitiesEntity.setAvater(userDao.queryAvatarById(item.getUserid()));
            postNewList.add(postActivitiesEntity);
        }


        return  postNewList;
    }


}
